
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>5.2.&nbsp;使用 from module import 导入模块</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;5&nbsp;章&nbsp;对象和面向对象">
      <link rel="previous" href="index.html" title="第&nbsp;5&nbsp;章&nbsp;对象和面向对象">
      <link rel="next" href="defining_classes.html" title="5.3.&nbsp;类的定义">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">对象和面向对象</a>&nbsp;&gt;&nbsp;<span class="thispage">使用 from module import 导入模块</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="index.html" title="上一页: “对象和面向对象”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="defining_classes.html" title="下一页: “类的定义”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="fileinfo.fromimport"></a>5.2.&nbsp;使用 <tt class="literal">from <i class="replaceable">module</i> import</tt> 导入模块
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p><span class="application">Python</span> 有两种导入模块的方法。两种都有用，你应该知道什么时候使用哪一种方法。一种方法，<tt class="literal">import <i class="replaceable">module</i></tt>，你已经在<a href="../getting_to_know_python/everything_is_an_object.html" title="2.4.&nbsp;万物皆对象">第&nbsp;2.4&nbsp;节 “万物皆对象”</a>看过了。另一种方法完成同样的事情，但是它与第一种有着细微但重要的区别。
            </p>
         </div>
         <div class="informalexample">
            <p>下面是 <tt class="literal">from <i class="replaceable">module</i> import</tt> 的基本语法：
            </p><pre class="programlisting"><span class='pykeyword'>
from</span> UserDict <span class='pykeyword'>import</span> UserDict
</pre></div>
         <p>它与你所熟知的 <a href="../getting_to_know_python/everything_is_an_object.html#odbchelper.import" title="例&nbsp;2.3.&nbsp;访问 buildConnectionString 函数的 doc string"><tt class="literal">import <i class="replaceable">module</i></tt></a> 语法很相似，但是有一个重要的区别：<tt class="filename">UserDict</tt> 被直接导入到局部名字空间去了，所以它可以直接使用，而不需要加上模块名的限定。你可以导入独立的项或使用 <tt class="literal">from <i class="replaceable">module</i> import *</tt> 来导入所有东西。
         </p><a name="compare.fromimport.perl"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%"><span class="application">Python</span> 中的 <tt class="literal">from <i class="replaceable">module</i> import *</tt> 像 <span class="application">Perl</span> 中的 <tt class="literal">use <i class="replaceable">module</i></tt> ；<span class="application">Python</span> 中的 <tt class="literal">import <i class="replaceable">module</i></tt> 像 <span class="application">Perl</span> 中的 <tt class="literal">require <i class="replaceable">module</i></tt> 。
               </td>
            </tr>
         </table><a name="compare.fromimport.java"></a><table class="note" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/note.png" alt="注意" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%"><span class="application">Python</span> 中的 <tt class="literal">from <i class="replaceable">module</i> import *</tt> 像 <span class="application">Java</span> 中的 <tt class="literal">import <i class="replaceable">module</i>.*</tt> ；<span class="application">Python</span> 中的 <tt class="literal">import <i class="replaceable">module</i></tt> 像 <span class="application">Java</span> 中的 <tt class="literal">import <i class="replaceable">module</i></tt> 。
               </td>
            </tr>
         </table>
         <div class="example"><a name="d0e11505"></a><h3 class="title">例&nbsp;5.2.&nbsp;<tt class="literal">import <i class="replaceable">module</i></tt> <span class="foreignphrase"><i class="foreignphrase"><span class="acronym">vs.</span></i></span> <tt class="literal">from <i class="replaceable">module</i> import</tt></h3><pre class="screen"><tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>import</span> types</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">types.FunctionType</span>             <a name="fileinfo.import.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">&lt;type 'function'&gt;</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">FunctionType</span>                   <a name="fileinfo.import.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="traceback">Traceback (innermost last):
  File "&lt;interactive input&gt;", line 1, in ?
NameError: There is no variable named 'FunctionType'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>from</span> types <span class='pykeyword'>import</span> FunctionType</span> <a name="fileinfo.import.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">FunctionType</span>                   <a name="fileinfo.import.1.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">&lt;type 'function'&gt;</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.import.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><tt class="filename">types</tt> 模块不包含方法，只是表示每种 <span class="application">Python</span> 对象类型的属性。注意这个属性必需用模块名 <tt class="filename">types</tt> 进行限定。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.import.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><tt class="constant">FunctionType</tt> 本身没有被定义在当前名字空间中；它只存在于 <tt class="filename">types</tt> 的上下文环境中。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.import.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">这个语法从 <tt class="filename">types</tt> 模块中直接将 <tt class="constant">FunctionType</tt> 属性导入到局部名字空间中。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#fileinfo.import.1.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">现在 <tt class="constant">FunctionType</tt> 可以直接使用，与 <tt class="filename">types</tt> 无关了。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>什么时候你应该使用 <tt class="literal">from <i class="replaceable">module</i> import</tt>？
         </p>
         <div class="itemizedlist">
            <ul>
               <li>如果你要经常访问模块的属性和方法，且不想一遍又一遍地敲入模块名，使用 <tt class="literal">from <i class="replaceable">module</i> import</tt>。
               </li>
               <li>如果你想要有选择地导入某些属性和方法，而不想要其它的，使用 <tt class="literal">from <i class="replaceable">module</i> import</tt>。
               </li>
               <li>如果模块包含的属性和方法与你的某个模块同名，你必须使用 <tt class="literal">import <i class="replaceable">module</i></tt> 来避免名字冲突。
               </li>
            </ul>
         </div>
         <p>除了这些情况，剩下的只是风格问题了，你会看到用两种方式编写的 <span class="application">Python</span> 代码。
         </p><a name="d0e11641"></a><table class="caution" border="0" summary="">
            <tr>
               <td rowspan="2" align="center" valign="top" width="1%"><img src="../images/caution.png" alt="小心" title="" width="24" height="24"></td>
            </tr>
            <tr>
               <td colspan="2" align="left" valign="top" width="99%">尽量少用 <tt class="literal">from module import *</tt> ，因为判定一个特殊的函数或属性是从哪来的有些困难，并且会造成调试和重构都更困难。
               </td>
            </tr>
         </table>
         <div class="furtherreading">
            <h3>进一步阅读关于模块导入技术</h3>
            <ul>
               <li><a href="http://www.effbot.org/guides/">eff-bot</a> 有更多关于 <a href="http://www.effbot.org/guides/import-confusion.htm"><tt class="literal">import <i class="replaceable">module</i></tt> <span class="foreignphrase"><i class="foreignphrase"><span class="acronym">vs.</span></i></span> <tt class="literal">from <i class="replaceable">module</i> import</tt></a> 的论述。
               </li>
               <li><a href="http://www.python.org/doc/current/tut/tut.html"><i class="citetitle"><span class="application">Python</span> Tutorial</i></a> 讨论了高级的导入技术，包括 <a href="http://www.python.org/doc/current/tut/node8.html#SECTION008410000000000000000"><tt class="literal">from <i class="replaceable">module</i> import *</tt></a>。
               </li>
            </ul>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="index.html">&lt;&lt;&nbsp;对象和面向对象</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#fileinfo.divein" title="5.1.&nbsp;概览">1</a> <span class="divider">|</span> <span class="thispage">2</span> <span class="divider">|</span> <a href="defining_classes.html" title="5.3.&nbsp;类的定义">3</a> <span class="divider">|</span> <a href="instantiating_classes.html" title="5.4.&nbsp;类的实例化">4</a> <span class="divider">|</span> <a href="userdict.html" title="5.5.&nbsp;探索 UserDict：一个封装类">5</a> <span class="divider">|</span> <a href="special_class_methods.html" title="5.6.&nbsp;专用类方法">6</a> <span class="divider">|</span> <a href="special_class_methods2.html" title="5.7.&nbsp;高级专用类方法">7</a> <span class="divider">|</span> <a href="class_attributes.html" title="5.8.&nbsp;类属性介绍">8</a> <span class="divider">|</span> <a href="private_functions.html" title="5.9.&nbsp;私有函数">9</a> <span class="divider">|</span> <a href="summary.html" title="5.10.&nbsp;小结">10</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="defining_classes.html">类的定义&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>